<?php

namespace app\admin\controller;


use think\Controller;
use think\Db;
use think\Request;
use think\Session;
class Admin extends Controller  //控制器
{
    /**
     * 获取管理员列表
     */
    public function index()
    {
    	if(!in_array(strrchr(__CLASS__."/".__FUNCTION__,"\\"),Session::get('admin_pri','admin'))){
        	 $this->error('你没有权限访问管理员列表', url('/main'), []);
        }
        
        $list = Db::name('rbac_admin')
            ->field(['id', 'username','email','is_use'])
            ->select();
            
        $role=Db::name('rbac_role')
            ->field(['id','role_name'])
            ->select();

        //模板
        return view('admin/index', [
            'title' => '管理员列表',
            'add' => '添加管理员',
            'list' => $list,
            'role' => $role,
        ]);
    }

    /**
     * 添加管理员页面
     */
    public function create()
    {
    	if(!in_array(strrchr(__CLASS__."/".__FUNCTION__,"\\"),Session::get('admin_pri','admin'))){
        	 $this->error('你没有权限添加管理员', url('/main'), []);
        }
        
    	 $list = Db::name('rbac_role')
            ->field(['id','role_name'])
            ->select();

        //模板
        return view('admin/create', [
            'list' => $list
        ]);
       
    }


    /**
     * 执行添加
     */
    public function docreate(Request $request)
    {
    	$p = $request->post();
        unset($p['role']);
        $role=Request::instance()->post('role/a'); 
        
        Db::startTrans();
        try{
        	Db::name('rbac_admin')
        	->insert($p);
           $userId = Db::name('rbac_admin')->getLastInsID();
           if(!empty($role)){
	        	  foreach($role as $v){
	        			$ar=array('admin_id'=>$userId,'role_id'=>$v);
	        			  $rows = Db::name('rbac_admin_role')
	           			 	->insert($ar);
	        		}
	        }
	        
	       Db::commit(); 
      		return json("成功");
        }catch(Exception $e){
        	Db::rollback();
        	return false;
        }
    }
    /**
     * 执行删除
     */
    public function del(Request $request){
    	if(!in_array(strrchr(__CLASS__."/".__FUNCTION__,"\\"),Session::get('admin_pri','admin'))){
        	 return "no pri";
        }
        
        $id=$request->get()['id'];
        
        
        Db::startTrans();
    	try{
    		
    		Db::name('rbac_admin')
    	 	->where('id','=',$id)
            ->delete();
        	Db::name('rbac_admin_role')->where('admin_id',$id)->delete();
        	Db::commit();
    		return true;
    		
    		
    	}catch(Exception $e){
    		Db::rollback();
    		
    		return false;
    	}
    	
       
    }
    
    
    
    
    /*
     * 获取用户信息
     */
    
    public function edit(Request $request){
    	if(!in_array(strrchr(__CLASS__."/".__FUNCTION__,"\\"),Session::get('admin_pri','admin'))){
        	return "no pri";
        }
        
    	$id=$request->get()['id'];
    	$res=Db::name('rbac_admin')
    		->where('id','=',$id)
            ->select();
        $data=$res[0];
        
        $role=Db::name('rbac_admin_role')
    		->where('admin_id','=',$id)
            ->select();
            
        if(!empty($role)){
        	foreach($role as $k=>$v){
        		$data['role'][]=$v['role_id'];
        	}
        }
            
     
        
        if(is_null($data)){
        	return false;
        }else{
        	 return json($data);
        }
    	
    	
    }
    
    /*
     * 编辑
     */
    
    public function doEdit(Request $request){
    	if(!in_array(strrchr(__CLASS__."/".__FUNCTION__,"\\"),Session::get('admin_pri','admin'))){
        	return "no pri";
        }
    	//插入用户表数据
    	$p = $request->post();
    	unset($p['role']);
    	$role=$request->post()['role'];
	    $id=$p['id'];
    	
    	Db::startTrans();
    	try{
    		
    		Db::name('rbac_admin')->update($p);
    		
    		
	    	Db::name('rbac_admin_role')->where('admin_id',$id)->delete();
	    	 if(!empty($role)){
		        	  foreach($role as $v){
		        			$ar=array('admin_id'=>$id,'role_id'=>$v);
		        			  $rows = Db::name('rbac_admin_role')
		           			 	->insert($ar);
		        		}
		        }
		    
		    Db::commit();
		    
		    return true;
    	} catch (Exception $e){
    		Db::rollback();
    		
    		return false;
    	}
    	
    	
    	
    	
    	
    }

}